/*
 * @Date: 2023-01-12 19:31:26
 * @LastEditors: Zireael 1193875164@qq.com
 * @LastEditTime: 2023-02-22 15:34:51
 * @FilePath: \A19\backend\src\api\Login\ApiTestEmail.ts
 */
import { ApiCall } from "tsrpc";
import { ReqTestEmail, ResTestEmail } from "../../shared/protocols/Login/PtlTestEmail";
import { Global } from "../../Global";
import { sendEmail } from "../../utils/sendEmail";
import { createCode } from "../../utils/createCode";

export default async function (call: ApiCall<ReqTestEmail, ResTestEmail>) {
    const email = call.req.email
    const user = await Global.collection('users').findOne({
        email
    })
    // if (user) return call.error('该邮箱已被注册过')

    try {
        const code = createCode()
        await sendEmail(email, code)

        call.succ({
            code
        })
    } catch (error) {
        console.log(error);
        call.error('发送失败')
    }

}